package warmachineroster.grapheditor.actions;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.ui.actions.EditorPartAction;
import org.eclipse.ui.IEditorPart;

import warmachineroster.UnitRosterElement;
import warmachineroster.grapheditor.command.UnitMinMaxSizeCommand;
import warmachineroster.grapheditor.editpart.UnitRosterElementEditPart;
import warmachinesystem.Unit;

public class MinMaxUnitSizeAction extends EditorPartAction {

	public static final String MINMAX = "MINMAX";
	
	public MinMaxUnitSizeAction(IEditorPart editor) {
		super(editor);
		setId(MINMAX);
		setText("minmax");
	}

	
	
	@Override
	public void run() {
		
		EditPart focus = getFocusedEditPart();
		
		UnitRosterElement ure = (UnitRosterElement) focus.getModel();
		
		UnitMinMaxSizeCommand command = new UnitMinMaxSizeCommand();
		command.setUnitRosterElement(ure);
		command.setLabel(this.getText());
		if (command.canExecute())
			execute(command);
		
	}



	private EditPart getFocusedEditPart() {
		IEditorPart ep = getEditorPart();
		
		GraphicalViewer epv = (GraphicalViewer) ep.getAdapter(GraphicalViewer.class);
		
		EditPart focus = epv.getFocusEditPart();
		return focus;
	}



	@Override
	protected boolean calculateEnabled() {
		return getFocusedEditPart() instanceof UnitRosterElementEditPart ;
	}

	@Override
	protected void refresh() {
		super.refresh();
		
		if (getFocusedEditPart() instanceof UnitRosterElementEditPart){
			UnitRosterElementEditPart unitEditPart = (UnitRosterElementEditPart) getFocusedEditPart();
			UnitRosterElement ure = unitEditPart.getUnitRosterElement();
			if (ure.getModelCount() == ((Unit)ure.getUnitType()).getBaseModelNumber()){
				this.setText("Max model count");
			} else {
				this.setText("Min model count");
			}
			
			
		}
		
	}

}
